package org.fastsyncer.manager;

import org.fastsyncer.cache.CacheService;
import org.fastsyncer.parser.Connector;
import org.fastsyncer.parser.Mapping;
import org.fastsyncer.parser.ParserFactory;
import org.fastsyncer.storage.StorageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author AE86
 * @version 2.0.0
 * @date 2019/9/16 23:59
 */
@Component
public class ManagerFactory implements Manager {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ParserFactory parserFactory;

    @Autowired
    private CacheService cacheService;

    @Autowired
    private StorageService storageService;

    @Override
    public boolean alive(String json) {
        return parserFactory.alive(json);
    }

    @Override
    public String saveConnector(String json) {
        // 1、解析连接器
        Connector connector = parserFactory.parseConnector(json);
        String id = connector.getId();
        logger.info("Parse connector:{}", id);

        // 2、持久化
        storageService.saveConnector(id, connector);
        logger.info("Stored connector:{}", id);

        // 3、放入缓存
        cacheService.put(id, connector);
        logger.info("Put connector into cache:{}", id);
        return id;
    }

    @Override
    public void removeConnector(String connectorId) {
        Assert.hasText(connectorId, "Connector id can not be empty.");
        cacheService.remove(connectorId);
        storageService.removeConnector(connectorId);
    }

    @Override
    public String saveMapping(String json) {
        // 1、解析驱动映射关系
        Mapping mapping = parserFactory.parseMapping(json);
        String id = mapping.getId();
        logger.info("Parse mapping:{}", id);

        // 2、持久化
        storageService.saveMapping(id, mapping);
        logger.info("Stored mapping:{}", id);

        // 3、放入缓存
        cacheService.put(id, mapping);
        logger.info("Put mapping into cache:{}", id);
        return id;
    }

    @Override
    public void removeMapping(String mappingId) {
        Assert.hasText(mappingId, "MappingId can not be empty.");
        cacheService.remove(mappingId);
        storageService.removeMapping(mappingId);
    }

    @Override
    public List<Connector> getConnectorAll() {
        List<Connector> list = new ArrayList<>();
        Map<String, Object> all = cacheService.getAll();
        if(CollectionUtils.isEmpty(all)){
            return list;
        }
        all.forEach((k,v) -> {
            if(v instanceof Connector){
                list.add((Connector) v);
            }
        });
        return list;
    }

    @Override
    public List<Mapping> getMappingAll() {
        List<Mapping> list = new ArrayList<>();
        Map<String, Object> all = cacheService.getAll();
        if(CollectionUtils.isEmpty(all)){
            return list;
        }
        all.forEach((k,v) -> {
            if(v instanceof Mapping){
                list.add((Mapping) v);
            }
        });
        return list;
    }

    @Override
    public void start(String mappingId) {

    }

    @Override
    public void stop(String mappingId) {

    }

}
